﻿using AutoMapper;
using ERPSystem.Common.Domains.supplier;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace BasicInformationWrite.API.BasicInformationCommons.suppliers
{
    public class PurchaseOrderUpdateHander : IRequestHandler<PurchaseOrderUpdateCommand, Result>
    {
        private readonly IBaseRepository<PurchaseOrder> purchaseOrderRe;
        private readonly IMapper mapper;

        public PurchaseOrderUpdateHander(IBaseRepository<PurchaseOrder> purchaseOrderRe, IMapper mapper)
        {
            this.purchaseOrderRe = purchaseOrderRe;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(PurchaseOrderUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Updated };

            var shippingnote = await (await purchaseOrderRe.GetAll()).Where(d => d.PurchaseOrderId == request.PurchaseOrderId).FirstAsync();

            if (shippingnote != null)
            {
                shippingnote.ReceiptNum = request.ReceiptNum;
                shippingnote.ReceiptState = request.ReceiptState;
                shippingnote.ReceiptDate = Convert.ToDateTime(request.ReceiptDate);
                shippingnote.BusinessType = request.BusinessType;
                shippingnote.ServiceStatus = request.ServiceStatus;
                shippingnote.Department = request.Department;
                shippingnote.Buyer = request.Buyer;
                shippingnote.Operator = request.Operator;
                shippingnote.Auditor = request.Auditor;
                shippingnote.ApprovalDate =Convert.ToDateTime(request.ApprovalDate) ;
                shippingnote.MaterialDemand = request.MaterialDemand;
                shippingnote.System = request.System;
                shippingnote.DeliveryDate = Convert.ToDateTime(request.DeliveryDate);
                shippingnote.PurchasingContract = request.PurchasingContract;
                shippingnote.Remark = request.Remark;
                shippingnote.OrderNumber = request.OrderNumber;
                shippingnote.MaterialCode = request.MaterialCode;
                shippingnote.MaterialName = request.MaterialName;
                shippingnote.Color = request.Color;
                shippingnote.DocumentType = request.DocumentType;
                shippingnote.RequirementDate = Convert.ToDateTime(request.RequirementDate);
                shippingnote.Quantity = request.Quantity;
                shippingnote.UnitPrice =Convert.ToDecimal(request.UnitPrice);
                shippingnote.Amount = Convert.ToDecimal(request.Amount);
                shippingnote.ArrivalQuantity = request.ArrivalQuantity;
                shippingnote.PurchaseDescription = request.PurchaseDescription;

                var res = await purchaseOrderRe.Update(shippingnote);
                if (res > 0)
                {
                    result.Msg = "修改成功";
                    result.Code = (int)CommonErrorCode.Success;
                }
                else
                {
                    result.Msg = "修改失败";
                    result.Code = (int)CommonErrorCode.Updated;
                }

            }
            return result;
        }
    }
}
